package com.example.waterdispenserm2.vmc;

import java.util.Arrays;

public class CommandBuilder {
    // 协议常量定义
    private static final byte HEADER1 = (byte) 0xAA;
    private static final byte HEADER2_START_WATER = (byte) 0x55;
    private static final byte HEADER2_OUTPUT = (byte) 0x22;
    private static final byte HEADER2_QUERY_SUM = (byte) 0x11;
    private static final byte HEADER2_QUERY_IO = (byte) 0x66;
    private static final byte HEADER2_QUERY_ERROR = (byte) 0x77;
    private static final byte FOOTER = (byte) 0xEE;

    /**
     * 构建出水/停水控制指令
     * @param start true:出水, false:停水
     * @param temp 温度设置(35-98℃) 如果是常温模式00
     * @param volume 水量设置(30-3000ml)
     * @return 指令字节数组
     */
    public static byte[] buildWaterControl(boolean start, int temp, int volume,int normal) {
        byte[] cmd = new byte[22];
        // 包头
        cmd[0] = HEADER1;
        cmd[1] = HEADER2_START_WATER;
        // 动作指令 (01:出水, 00:停水)
        cmd[2] = start ? (byte) 0x01 : (byte) 0x00;
        //温度
        if (normal==1){//常温
            cmd[3] = (byte)0x00;
        }else {
            // 温度设置 (35-98℃)
            cmd[3] = (byte) Math.max(35, Math.min(98, temp));
        }

        // 水量设置 (高位在前)
        volume = Math.max(30, Math.min(3000, volume)); // 限制范围
        cmd[4] = (byte) ((volume >> 8) & 0xFF); // 高字节
        cmd[5] = (byte) (volume & 0xFF);        // 低字节
        // 预留位填充0
        Arrays.fill(cmd, 6, 19, (byte) 0x00);
        // 计算校验和
        setChecksum(cmd, 19);
        // 包尾
        cmd[21] = FOOTER;
        return cmd;
    }

    /**
     * 构建输出控制指令
     * @param config 输出配置对象
     * @return 指令字节数组
     */
    public static byte[] buildOutputControl(OutputConfig config) {
        byte[] cmd = new byte[16];
        cmd[0] = HEADER1;
        cmd[1] = HEADER2_OUTPUT;
        // UV灯控制 (00:关, 01/03:开)
        cmd[2] = config.output1;
        // 进水阀控制
        cmd[3] = config.output2;
        // 冲洗阀控制 (净水版)
        cmd[4] = config.output3;
        // 增压泵控制 (净水版)
        cmd[5] = config.output4;
        // 预留位
        Arrays.fill(cmd, 6, 13, (byte) 0x00);
        // 校验和
        setChecksum(cmd, 13);
        // 包尾
        cmd[15] = FOOTER;
        return cmd;
    }

    /**
     * 构建IO状态查询指令
     * @return 指令字节数组
     */
    public static byte[] buildQueryIO() {
        byte[] cmd = new byte[16];
        cmd[0] = HEADER1;
        cmd[1] = HEADER2_QUERY_IO;
        Arrays.fill(cmd, 2, 13, (byte) 0x00);
        setChecksum(cmd, 13);
        cmd[15] = FOOTER;
        return cmd;
    }

    /**
     * 构建累计数据查询指令
     * @return 指令字节数组
     */
    public static byte[] buildQuerySummary() {
        byte[] cmd = new byte[16];
        cmd[0] = HEADER1;
        cmd[1] = HEADER2_QUERY_SUM;
        Arrays.fill(cmd, 2, 13, (byte) 0x00);
        setChecksum(cmd, 13);
        cmd[15] = FOOTER;
        return cmd;
    }

    /**
     * 构建错误查询指令
     * @return 指令字节数组
     */
    public static byte[] buildQueryError() {
        byte[] cmd = new byte[16];
        cmd[0] = HEADER1;
        cmd[1] = HEADER2_QUERY_ERROR;
        Arrays.fill(cmd, 2, 13, (byte) 0x00);
        setChecksum(cmd, 13);
        cmd[15] = FOOTER;
        return cmd;
    }

    /**
     * 计算并设置校验和
     * @param data 指令数据
     * @param checksumIndex 校验和起始位置
     */
    private static void setChecksum(byte[] data, int checksumIndex) {
        int sum = 0;
        for (int i = 0; i < checksumIndex; i++) {
            sum += data[i] & 0xFF; // 转换为无符号整数
        }
        data[checksumIndex] = (byte) ((sum >> 8) & 0xFF); // 校验和高位
        data[checksumIndex + 1] = (byte) (sum & 0xFF);    // 校验和低位
    }

    /**
     * 输出配置类
     * 封装各输出通道的状态
     */
    public static class OutputConfig {
        public byte output1 = 0; // UV灯: 0=关, 1=半驱, 3=全驱
        public byte output2 = 0; // 进水阀: 0=关, 1=半驱, 3=全驱
        public byte output3 = 0; // 冲洗阀: 0=关, 1=半驱, 3=全驱
        public byte output4 = 0; // 增压泵: 0=关, 1=开
    }
}